﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;

using ProgramMain.Accounting;
using ProgramMain.Clients;
using ProgramMain.Diagnostics;
using ProgramMain.Equipments;
using ProgramMain.Framework;
using ProgramMain.Operators;
using ProgramMain.Parameters;
using ProgramMain.PhoneDirections;
using ProgramMain.PhoneTariffPlans;
using ProgramMain.Reports;
using ProgramMain.Skins;

namespace ProgramMain
{
	public partial class FrmMain : DevExpress.XtraEditors.XtraForm
	{
        public string ScreenDeviceName;
        
        public FrmMain()
		{
			InitializeComponent();

            this.Icon = Framework.GlobalData.DefaultIcon();
            
            InitSkins();

            InitTabbedMDI();
		}

		#region Skins

		private void InitSkins()
		{
			barManager1.ForceInitialize();
			
            foreach (DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins)
			{
                if (cnt.SkinName != DevExpress.LookAndFeel.UserLookAndFeel.DefaultSkinName)
                {
                    BarButtonItem item = new BarButtonItem(barManager1, cnt.SkinName);
                    mnuPaintStyle.AddItem(item);
                    item.ItemClick += new ItemClickEventHandler(OnSkinClick);
                }
			}

            SetSkinCaption();
        }

		private void OnSkinClick(object sender, ItemClickEventArgs e)
		{
            ApplicationSkins.SetSkinSettings(e.Item.Caption);
            SetSkinCaption();
		}

        private void SetSkinCaption()
        {
            string skinName = ApplicationSkins.GetSkinSettings();

            barManager1.GetController().PaintStyleName = "Skin";
            mnuPaintStyle.Caption = skinName;
            mnuPaintStyle.Hint = mnuPaintStyle.Caption;
            mnuPaintStyle.ImageIndex = -1;
        }

		#endregion

        #region Window placing

        private Boolean IsTabbedMdi 
        { 
            get { return mnuTabbed.Down; } 
        }

        void InitTabbedMDI()
        {
            mdiManager1.MdiParent = IsTabbedMdi ? this : null;
            mnuCascade.Visibility = mnuHorizontal.Visibility = mnuVertical.Visibility = IsTabbedMdi ? BarItemVisibility.Never : BarItemVisibility.Always;
        }


        private void mnuTabbed_ItemClick(object sender, ItemClickEventArgs e)
        {
            InitTabbedMDI();
        }


        private void mnuCascade_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }


        private void mnuHorizontal_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }


        private void mnuVertical_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        #endregion

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Version ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            this.Text = GlobalData.GetAssemblyTitle() + " v " + ver.ToString(2) + " - [" + BaseSecurity.UserName + "]";
            
            if (Properties.Settings.Default.NavPaneExpanded)
                nbcMain.OptionsNavPane.NavPaneState = NavPaneState.Expanded;
            else
                nbcMain.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
        }


        private void FrmMain_Shown(object sender, EventArgs e)
        {
            Screen ProgramScreen = Screen.FromControl(Program.MainForm);
            ScreenDeviceName = ProgramScreen.DeviceName;
        }

        private void nbcMain_NavPaneStateChanged(object sender, EventArgs e)
        {
            bool Expaned;
            if (nbcMain.OptionsNavPane.NavPaneState == NavPaneState.Expanded)
                Expaned = true;
            else
                Expaned = false;
            if (Expaned != Properties.Settings.Default.NavPaneExpanded)
            {
                Properties.Settings.Default.NavPaneExpanded = Expaned;
                Properties.Settings.Default.Save();
            }
        }

        private void mnuExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void mnuAbout_ItemClick(object sender, ItemClickEventArgs e)
        {
            nbiAbout_LinkClicked(sender, new DevExpress.XtraNavBar.NavBarLinkEventArgs(this.nbgHelp.ItemLinks[0]));
        }

        private void nbiAbout_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            FrmAbout frm = new FrmAbout();
            frm.ShowDialog(Program.MainForm);
        }

        private void mnuUpdate_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (GlobalData.UpdateApplication() == false)
            {
                XtraMessageBox.Show("Установлена последняя версия приложения", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void timerUpdateCheck_Tick(object sender, EventArgs e)
        {
            GlobalData.UpdateApplication();
        }

        private void nbiDirections_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string DirIdent = frmPhoneDirEditor.GeneratePhoneDirectionIdent();
            frmPhoneDirEditor tmp = (frmPhoneDirEditor)frmPhoneDirEditor.FindForm(DirIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityViewAction())
                {
                    frmPhoneDirEditor frmPhoneDirs = new frmPhoneDirEditor();
                    frmPhoneDirs.ShowAsMDI(this);
                }
            }
        }

        private void niRounds_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string RoundIdent = frmPhoneRoundList.GeneratePhoneRoundIdent();
            frmPhoneRoundList tmp = (frmPhoneRoundList)frmPhoneRoundList.FindForm(RoundIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityViewAction())
                {
                    frmPhoneRoundList frmRounds = new frmPhoneRoundList();
                    frmRounds.ShowAsMDI(this);
                }
            }
        }

        private void nbiHolidays_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string holidayIdent = frmHolidayList.GenerateHolidayIdent();
            frmHolidayList tmp = (frmHolidayList)frmHolidayList.FindForm(holidayIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmHolidayList frmHolidays = new frmHolidayList();
                    frmHolidays.ShowAsMDI(this);
                }
            }
        }

        private void nbiClients_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string ClientIdent = frmClientList.GenerateClientsIdent();
            frmClientList tmp = (frmClientList)frmClientList.FindForm(ClientIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityViewAction())
                {
                    frmClientList frmClient = new frmClientList();
                    frmClient.ShowAsMDI(this);
                }
            }
        }

        private void nbiOperators_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string OperatorIdent = frmOperatorList.GenerateOperatorListIdent();
            frmOperatorList tmp = (frmOperatorList)frmOperatorList.FindForm(OperatorIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmOperatorList OperatorList = new frmOperatorList();
                    OperatorList.ShowAsMDI(this);
                }
            }
        }

        private void nbiTruncInfo_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string TruncInfoIdent = frmTruncInfoList.GenerateTruncInfoIdent();
            frmTruncInfoList tmp = (frmTruncInfoList)frmTruncInfoList.FindForm(TruncInfoIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmTruncInfoList frmTruncInfo = new frmTruncInfoList();
                    frmTruncInfo.ShowAsMDI(this);
                }
            }
        }

        private void nbiSuperTariff_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string SuperTariffIdent = frmSuperTariffList.GenerateSuperTariffIdent();
            frmSuperTariffList tmp = (frmSuperTariffList)frmSuperTariffList.FindForm(SuperTariffIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmSuperTariffList frmSuperTariff = new frmSuperTariffList();
                    frmSuperTariff.ShowAsMDI(this);
                }
            }
        }

        private void nbiBindTariffPlans_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string SuperTariffIdent = frmSuperTariffObjectList.GenerateSuperTariffIdent();
            frmSuperTariffObjectList tmp = (frmSuperTariffObjectList)frmSuperTariffObjectList.FindForm(SuperTariffIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmSuperTariffObjectList frmSuperTariff = new frmSuperTariffObjectList();
                    frmSuperTariff.ShowAsMDI(this);
                }
            }
        }

        private void nbiPrefixes_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string PhonePrefixesIdent = frmPhonePrefixesList.GeneratePhonePrefixesIdent();
            frmPhonePrefixesList tmp = (frmPhonePrefixesList)frmPhonePrefixesList.FindForm(PhonePrefixesIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmPhonePrefixesList frmPhonePrefixes = new frmPhonePrefixesList();
                    frmPhonePrefixes.ShowAsMDI(this);
                }
            }
        }

        private void nbiBilling_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string BillingIdent = frmBillingCalc.GenerateClientSummaryIdent();
            frmBillingCalc tmp = (frmBillingCalc)frmBillingCalc.FindForm(BillingIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmBillingCalc frmBilling = new frmBillingCalc();
                    frmBilling.ShowAsMDI(this);
                }
            }
        }

        private void nbiBillingExport_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string BillingExportListIdent = frmBillingExportList.GenerateClientSummaryIdent();
            frmBillingExportList tmp = (frmBillingExportList)frmBillingExportList.FindForm(BillingExportListIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmBillingExportList frmBillingExport = new frmBillingExportList();
                    frmBillingExport.ShowAsMDI(this);
                }
            }
        }

        private void nbiDiagnostics_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string DiagnosticIdent = frmCallHistoryWarningTree.GenerateCallHistoryWarningTreeIdent();
            frmCallHistoryWarningTree tmp = (frmCallHistoryWarningTree)frmCallHistoryWarningTree.FindForm(DiagnosticIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmCallHistoryWarningTree frmDiagnostic = new frmCallHistoryWarningTree();
                    frmDiagnostic.ShowAsMDI(this);
                }
            }
        }

        private void mnuParameters_ItemClick(object sender, ItemClickEventArgs e)
        {
            ProgramParametersCtl frmParameters = new ProgramParametersCtl();
            frmParameters.ShowModal();
        }

        private void nbiLogDetail_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string LogDetailIdent = frmLogDetailList.GenerateLogDetailIdent();
            frmLogDetailList tmp = (frmLogDetailList)frmLogDetailList.FindForm(LogDetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmLogDetailList frmLogDetail = new frmLogDetailList();
                    frmLogDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiRecurringAndResources_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string DetailIdent = frmRecurringAndResources.GenerateRecurringAndResourcesIdent();
            frmRecurringAndResources tmp = (frmRecurringAndResources)frmRecurringAndResources.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmRecurringAndResources frmDetail = new frmRecurringAndResources();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void mnuLogout_ItemClick(object sender, ItemClickEventArgs e)
        {
            Properties.Settings.Default.UserName = "";
            Properties.Settings.Default.UserPassword = "";
            Properties.Settings.Default.Save();
            Application.Restart();
        }

        private void nbiEquipmentTraffic_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string DetailIdent = frmCallsByEquip.GenerateRecurringAndResourcesIdent();
            frmCallsByEquip tmp = (frmCallsByEquip)frmCallsByEquip.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmCallsByEquip frmDetail = new frmCallsByEquip();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiServiceTypeMap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string DetailIdent = frmServiceTypeMap.GenerateServiceTypeMapIdent();
            frmServiceTypeMap tmp = (frmServiceTypeMap)frmServiceTypeMap.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityViewAction())
                {
                    frmServiceTypeMap frmDetail = new frmServiceTypeMap();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiEquipments_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmEquipmentList.GenerateEquipmentIdent();
            frmEquipmentList tmp = (frmEquipmentList)frmEquipmentList.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmEquipmentList frmDetail = new frmEquipmentList();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiEmailList_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmEmailList.GenerateEmailListIdent();
            frmEmailList tmp = (frmEmailList)frmEmailList.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecuritySuperAction())
                {
                    frmEmailList frmDetail = new frmEmailList();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiCallByZone_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmCallByZone.GenerateCallByZoneIdent();
            frmCallByZone tmp = (frmCallByZone)frmCallByZone.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmCallByZone frmDetail = new frmCallByZone();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }


        private void nbiOperatorCallByZone_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmOperatorCallByZone.GenerateCallByZoneIdent();
            frmOperatorCallByZone tmp = (frmOperatorCallByZone)frmOperatorCallByZone.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmOperatorCallByZone frmDetail = new frmOperatorCallByZone();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiDirectionWarningReport_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmDirectionWarnings.GenerateDirectionWarningsIdent();
            frmDirectionWarnings tmp = (frmDirectionWarnings)frmDirectionWarnings.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmDirectionWarnings frmDetail = new frmDirectionWarnings();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }

        private void nbiUnassignedPhones_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            string DetailIdent = frmUnassignedPhones.GenerateUnassignedPhonesIdent();
            frmUnassignedPhones tmp = (frmUnassignedPhones)frmUnassignedPhones.FindForm(DetailIdent);
            if (tmp != null)
            {
                tmp.Activate();
            }
            else
            {
                if (Framework.BaseSecurity.CheckSecurityTechAction())
                {
                    frmUnassignedPhones frmDetail = new frmUnassignedPhones();
                    frmDetail.ShowAsMDI(this);
                }
            }
        }
	}
}